import Mock from 'mockjs'
import { hasOwnProp } from '../../utils/normalUtils'
import userMock from './userMock'
import onLineMock from './onLineMock'

/**
 * response 内对象
 * @param data
 * @param code
 * @param message
 */
export function responseDataMock(data:any, code = 'SUCCESS', message = 'success') {
    return {
        code,
        message,
        data
    }
}
/**
 * response 内对象
 * @param list
 * @param code
 * @param message
 */
export function pageDataMock(list:any, code = 'SUCCESS', message = 'success') {
    return {
        total: 200,
        rows: list
    }
}

export function mockMap(apiMap:any, baseUrl = '') {
    const mockList = []
    for (const att in apiMap) {
        if (att !== 'baseUrl') {
            const arr1 = att.split(':')
            const apiData:any = apiMap[att]
            if (typeof apiData === 'function') {
                mockList.push({
                    url: baseUrl + arr1[1],
                    method: arr1[0],
                    timeout: 300,
                    response: (params:any) => {
                        const data:any = apiMap[att](params)
                        const mockData = Mock.mock(data)
                        if (data && data.orgain === true) return mockData
                        return responseDataMock(mockData)
                    }
                })
            } else {
                const obj = {
                    url: (hasOwnProp(apiData, 'baseUrl') ? apiData.baseUrl : baseUrl) + arr1[1],
                    method: arr1[0],
                    timeout: 300
                }
                Object.assign(obj, apiData)
                mockList.push(obj)
            }
        }
    }
    return mockList
}

export function mockList(configList:any[]) {
    const arr = []
    for (const item of configList) {
      if (item) arr.push(...mockMap(item, hasOwnProp(item, 'baseUrl') ? (item.baseUrl || '') : ''))
    }
    return arr
}



const commonMock = {
    baseUrl: '/user',
    "get:/test/ok":()=>{
        return  responseDataMock({})
    },
    "post:/test/ok":()=>{
        return  responseDataMock({})
    },
    "put:/test/ok":()=>{
        return  responseDataMock({})
    },
    "delete:/test/ok":()=>{
        return  responseDataMock({})
    }
}

export default mockList([
    commonMock,
    userMock,
    onLineMock
])
